package karaoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.modelo.Persona;
import karaoke.util.ConexionBD;

public class ClienteDAO extends BaseDAO{

	public Cliente registrarCuentaCliente(Cliente c) throws DAOExcepcion{
		PersonaDAO pdao = new PersonaDAO();
		Persona p = pdao.insertar(c);
		
		String query = "insert into cliente(correo, clave, telefono, dni) values(?, ?, ?, ?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, c.getCorreo());
			stmt.setString(2, c.getClave());
			stmt.setString(3, c.getTelefono());
			stmt.setString(4, p.getDni());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			c.setIdcliente(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public Cliente obtener(String correo, String clave) throws DAOExcepcion{
		Cliente c = new Cliente();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select p.*,c.idcliente,c.correo,c.clave,c.telefono from cliente c inner join persona p on c.dni=p.dni where correo=? and clave=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, correo);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();
			if (rs.next()) {
				c.setIdcliente(rs.getInt("idcliente"));
				c.setNombres(rs.getString("nombres"));
				c.setPaterno(rs.getString("paterno"));
				c.setMaterno(rs.getString("materno"));
				c.setCorreo(rs.getString("correo"));
				c.setClave(rs.getString("clave"));
				c.setTelefono(rs.getString("telefono"));
				c.setDni(rs.getString("dni"));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	public Cliente obtenerPorID(int idcliente) throws DAOExcepcion{
		Cliente c = new Cliente();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select p.*,c.idcliente,c.correo,c.clave,c.telefono from cliente c inner join persona p on c.dni=p.dni where c.idcliente=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idcliente);
			rs = stmt.executeQuery();
			if (rs.next()) {
				c.setIdcliente(rs.getInt("idcliente"));
				c.setNombres(rs.getString("nombres"));
				c.setPaterno(rs.getString("paterno"));
				c.setMaterno(rs.getString("materno"));
				c.setCorreo(rs.getString("correo"));
				c.setClave(rs.getString("clave"));
				c.setTelefono(rs.getString("telefono"));
				c.setDni(rs.getString("dni"));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public Collection<Cliente> listar()
			throws DAOExcepcion {
		String query = "select p.*,c.idcliente,c.correo,c.clave,c.telefono from cliente c inner join persona p on c.dni=p.dni ";
		Collection<Cliente> lista = new ArrayList<Cliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cliente c = new Cliente();
				c.setIdcliente(rs.getInt("idcliente"));
				c.setNombres(rs.getString("nombres"));
				c.setPaterno(rs.getString("paterno"));
				c.setMaterno(rs.getString("materno"));
				c.setCorreo(rs.getString("correo"));
				c.setClave(rs.getString("clave"));
				c.setTelefono(rs.getString("telefono"));
				c.setDni(rs.getString("dni"));
				
				lista.add(c);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
	
	public Collection<Cliente> buscarPorDniCorreo(String dni, String correo)
			throws DAOExcepcion {
		String query = "select p.*,c.idcliente,c.correo,c.clave,c.telefono from cliente c inner join persona p on c.dni=p.dni where p.dni=? or correo=?";
		Collection<Cliente> lista = new ArrayList<Cliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dni);
			stmt.setString(2, correo);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cliente c = new Cliente();
				c.setIdcliente(rs.getInt("idcliente"));
				c.setNombres(rs.getString("nombres"));
				c.setPaterno(rs.getString("paterno"));
				c.setMaterno(rs.getString("materno"));
				c.setCorreo(rs.getString("correo"));
				c.setClave(rs.getString("clave"));
				c.setTelefono(rs.getString("telefono"));
				c.setDni(rs.getString("dni"));
				
				lista.add(c);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
}
